package apis

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"strings"
	"vrcm/src/vrcm/app/admin/models/live_schedule"
	dto "vrcm/src/vrcm/app/admin/service/dto/live_schedule"
	service "vrcm/src/vrcm/app/admin/service/live_schedule"
	"vrcm/src/vrcm/common/apis"
)

type WaterMarkTrans struct {
	apis.Api
}

func (e WaterMarkTrans) GetPage(c *gin.Context) {
	req := dto.WatermarkTemplateGetPageReq{}
	s := service.WaterMarkTaskService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	list := make([]live_schedule.WaterMarkTemplate, 0, 5)
	var count int64

	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取水印模版失败， %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

func (e WaterMarkTrans) Get(c *gin.Context) {
	req := dto.WatermarkTemplateGetReq{}
	s := service.WaterMarkTaskService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetTemplate(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取水印模版失败， %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}

func (e WaterMarkTrans) Insert(c *gin.Context) {
	req := dto.WatermarkTemplateInsertReq{}
	s := service.WaterMarkTaskService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.InsertTemplate(&req)

	if err != nil {
		if strings.Contains(err.Error(), "Duplicate entry") {
			e.Error(apis.AddSourceError.Code, err, fmt.Sprintf("创建水印模版失败， %s", "不能重复创建"))
		} else {
			e.Error(apis.AddSourceError.Code, err, fmt.Sprintf("创建水印模版失败， %s", err.Error()))
		}
		return
	}

	e.OK(req.Id, "创建成功")
}

func (e WaterMarkTrans) Update(c *gin.Context) {
	req := dto.WatermarkTemplateUpdateReq{}
	s := service.WaterMarkTaskService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	err = s.UpdateTemplate(&req)
	if err != nil {
		e.Error(apis.UpdateChannelError.Code, err, fmt.Sprintf("修改水印模版失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.Id, "修改成功")
}

func (e WaterMarkTrans) Delete(c *gin.Context) {
	s := service.WaterMarkTaskService{}
	req := dto.WatermarkTemplateDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.DeleteTemplate(&req)
	if err != nil {
		e.Error(apis.DeleteChannelError.Code, err, fmt.Sprintf("删除任务失败，%s", err.Error()))
		return
	}
	e.OK(req.Id, "删除成功")
}

func (e WaterMarkTrans) WaterMarkCallBack(c *gin.Context) {
	s := &service.WaterMarkTaskService{}
	req := dto.TargetCallBackReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	e.Logger.Trace("水印转码状态回调任务", req)
	err = s.WaterMarkCallBack(&req)
	if err != nil {
		e.Error(apis.CallBackError.Code, err, fmt.Sprintf("回调接口失败， %s", err.Error()))
		return
	}
	//c.Writer.Header().Set("Content-Length", "-1")
	e.OK("", "水印转码状态回调接口成功")
}
